//
//  SearchResultViewController.swift
//  Hermes
//
//  Created by FH on 2021/1/26.
//  Copyright © 2021 Agora. All rights reserved.
//

import Foundation

// TODO: remove
class SearchResultViewControllerTODOREMOVE : NSViewController {
    let yoga = FLContext()
    var properties: ConfigScriptContext! = nil
    var table: FLTableView<AstNode, SearchResultRowCellView>! = nil
    
    deinit {
        print(">>> SearchResultViewController dealloc ...")
    }
    
    convenience init(_ properties: ConfigScriptContext) {
        self.init(nibName: nil, bundle: nil)
        self.properties = properties
    }
    
    override func loadView() {
        self.view = NSView(frame: CGRect(x: 0, y: 0, width: 520, height: 700))
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.bindAsRoot(to: yoga)
        
        self.table = buildRowCellTableView(yoga, SearchResultRowCellView.self)
        self.table.bind(to: yoga, self.view) { it in
            it.flexGrow = 1
        }
        yoga.apply()
        
        properties.$searchDataSource.signal().subscribeNext {[weak self] data in
            if let self = self {
                self.table.reload(columnData: nil, rowData: self.properties.searchDataSource)
            }
        }
        
        self.table.rac_eventSelectItem.subscribeNext {[weak self] (node, _) in
            if let self = self {
                self.properties.setSearchKey(node.name)
            }
        }
    }
}
